package com.briup.jdk8;

import java.util.*;
import java.util.function.IntFunction;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Stream 转其他
 */
public class StreamTo {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("海鹏", "海螺", "海星", "海燕", "海绵宝宝");
        Object[] array = stream.toArray();
        System.out.println(Arrays.toString(array));
        stream = Stream.of("海鹏", "海螺", "海星", "海燕", "海绵宝宝");
//        String[] strings = stream.toArray(new IntFunction<String[]>() {
//            @Override
//            public String[] apply(int value) {
//                return new String[value];
//            }
//        });
//        String[] strings = stream.toArray(v->new  String[v]);
        String[] strings = stream.toArray(String[]::new);
        System.out.println(Arrays.toString(strings));
        //Stream转集合  ---list
        stream = Stream.of("海鹏", "海螺", "海星", "海燕", "海绵宝宝");
        List<String> list = stream.collect(Collectors.toList());
        System.out.println(list);
        stream = Stream.of("海鹏", "海螺", "海星", "海燕", "海绵宝宝");
//        Collection<String> list1 = stream.collect(Collectors.toCollection(new Supplier<Collection<String>>() {
//            @Override
//            public Collection<String> get() {
//                return new ArrayList<String>();
//            }
//        }));
//        ArrayList<String> list1 = stream.collect(Collectors.toCollection(() -> new ArrayList<>()));
        ArrayList<String> list1 = stream.collect(Collectors.toCollection(ArrayList::new));
        System.out.println(list1);
        //Stream转集合  ---set
        stream = Stream.of("海鹏", "海螺", "海星", "海燕", "海绵宝宝");
        Set<String> set = stream.collect(Collectors.toSet());
        System.out.println(set);
        stream = Stream.of("海鹏", "海螺", "海星", "海燕", "海绵宝宝");
//        stream.collect(Collectors.toCollection(new Supplier<Collection< String>>() {
//            @Override
//            public Collection<String> get() {
//                return new HashSet();
//            }
//        }));
//        stream.collect(Collectors.toCollection(()->new HashSet<>()));
        stream.collect(Collectors.toCollection(HashSet::new));

        //Stream转置到String
        stream = Stream.of("海鹏", "海螺", "海星", "海燕", "海绵宝宝");
        String s = stream.collect(Collectors.joining());
        System.out.println(s);
        stream = Stream.of("海鹏", "海螺", "海星", "海燕", "海绵宝宝");
        String s1 = stream.collect(Collectors.joining("-"));
        System.out.println(s1);
        stream = Stream.of("海鹏", "海螺", "海星", "海燕", "海绵宝宝");
        System.out.println(stream.getClass());
        String s2 = stream.collect(Collectors.joining("-","briup","adam"));
        System.out.println(s2);



    }
}
